// Fill out your copyright notice in the Description page of Project Settings.

#include "Isalong2.h"
#include <algorithm>
#include "WeaponBase.h"


// Sets default values
AWeaponBase::AWeaponBase()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	_dataPackSaver = new FWeaponPack();
	_dataPack = _dataPackSaver;

	_skMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MainMesh"));
	_particleSys = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("Particle"));
	_forwardArrow = CreateDefaultSubobject<UArrowComponent>(TEXT("Direction"));

	RootComponent = _skMesh;
	//_skMesh->AttachTo(RootComponent);
	_particleSys->AttachTo(_skMesh);
	_forwardArrow->AttachTo(_skMesh);

	_skMesh->bSelfShadowOnly = true;

	_particleSys->bAutoActivate = false;
}

AWeaponBase::~AWeaponBase()
{
	delete _dataPackSaver;
}

// Called when the game starts or when spawned
void AWeaponBase::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AWeaponBase::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
}

void AWeaponBase::OutlineOn()
{
	_skMesh->bRenderCustomDepth = true;
	_skMesh->SetCustomDepthStencilValue(255);
}

void AWeaponBase::OutlineOff()
{
	_skMesh->bRenderCustomDepth = false;
	_skMesh->SetCustomDepthStencilValue(0);
}

int AWeaponBase::GetBulletContains()
{
	return	_bulletContains;
}

int AWeaponBase::GetMaxBulletContains()
{
	return _dataPack->_maxContains;
}

void AWeaponBase::SetBulletContains(int num, bool dispatch)
{
	_bulletContains = num;
	if (dispatch)
	{
		for (auto i : _onChange)
		{
			i->OnGunBulletChange(_bulletContains);
		}
	}
}

void AWeaponBase::BindOnGunBulletChange(ICallBack* func)
{
	_onChange.push_back(func);
}

void AWeaponBase::DeBindOnGunBulletChange(ICallBack* func)
{
	_onChange.erase(std::remove(_onChange.begin(), _onChange.end(), func), _onChange.end());
}

FWeaponPack AWeaponBase::GetWeaponPackData()
{
	return *_dataPack;
}

void AWeaponBase::PickUp(IWeaponOwner* character)
{
	if (_weaponOwner!=nullptr)
	{
		return;
	}
	_weaponOwner = character;
	_canAsTookUpItem = false;
}

void AWeaponBase::PutDown(IWeaponOwner* character)
{
	if (_weaponOwner != character)
	{
		return;
	}
	_weaponOwner = nullptr;
	_canAsTookUpItem = true;
}

int AWeaponBase::GetWeaponType()
{
	return _itemType;
}

bool AWeaponBase::IsFull()
{
	if (_bulletContains==_dataPack->_maxContains)
	{
		return true;
	}
	return false;
}
